<template>
  <div id="userRegisterPage">
    <div class="register-container">
      <!-- 左侧品牌展示区 -->
      <div class="brand-section">
        <div class="brand-content">
          <!-- 展示LOGO -->
          <img
            src="../assets/logo3.jpg"
            alt="云枢智创"
            class="brand-logo"
            style="width: 200px; height: 100px; margin-bottom: 20px"
          />
          <h2 class="brand-subtitle">创新服务平台</h2>
          <div class="brand-dots">
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
          </div>
          <p class="brand-slogan">汇聚创新智慧，培育未来人才</p>
          <p class="brand-mission">科技创新引领，服务成就未来</p>
        </div>
      </div>

      <!-- 右侧注册表单 -->
      <div class="register-form-section">
        <div class="form-container">
          <h2 class="form-title">
            <span class="title-primary">云枢智创</span>
            <span class="title-secondary">创新服务平台</span>
          </h2>
          <h3 class="form-subtitle">账号注册</h3>

          <a-form :model="registerForm" @finish="handleRegister" class="login-form">
            <!-- 用户类型切换 -->
            <a-form-item>
              <a-radio-group v-model:value="registerForm.isTeacher" class="login-type-switch">
                <a-radio :value="0">学生注册</a-radio>
                <a-radio :value="1">教师注册</a-radio>
              </a-radio-group>
            </a-form-item>

            <div class="register-form-row">
              <a-form-item
                label="用户名"
                name="name"
                :rules="[{ required: true, message: '请输入真实姓名!' }]"
              >
                <a-input v-model:value="registerForm.name" placeholder="请输入真实姓名" />
              </a-form-item>

              <a-form-item
                :label="registerForm.isTeacher === 1 ? '工号' : '学号'"
                name="number"
                :rules="[{ required: true, message: '请输入学号/工号!' }]"
              >
                <a-input
                  v-model:value="registerForm.number"
                  :placeholder="registerForm.isTeacher === 1 ? '请输入工号' : '请输入学号'"
                />
              </a-form-item>
            </div>

            <a-form-item
              label="学院"
              name="college"
              :rules="[{ required: true, message: '请选择学院!' }]"
            >
              <a-select
                v-model:value="registerForm.college"
                placeholder="请选择学院"
                :options="collegeOptions"
              />
            </a-form-item>

            <a-form-item
              label="邮箱"
              name="email"
              :rules="[{ required: true, message: '请输入邮箱!' }]"
            >
              <a-input v-model:value="registerForm.email" placeholder="请输入邮箱" />
            </a-form-item>

            <a-form-item
              label="密码"
              name="password"
              :rules="[{ required: true, message: '请输入密码!' }]"
            >
              <a-input-password v-model:value="registerForm.password" placeholder="请输入密码" />
            </a-form-item>

            <a-form-item name="code">
              <div style="display: flex; align-items: center; justify-content: space-between">
                <a-input
                  v-model:value="registerForm.code"
                  placeholder="请输入验证码"
                  class="login-input"
                  style="flex: 1; margin-right: 10px"
                />
                <a-button
                  type="primary"
                  class="register-button"
                  style="width: auto"
                  @click="sendCode"
                >
                  {{ countdown > 0 ? `${countdown}秒` : '获取验证码' }}
                </a-button>
              </div>
            </a-form-item>

            <!-- <a-form-item>
              <a-input
                v-model:value="registerForm.loginValue"
                :placeholder="registerForm.isTeacher === 1 ? '请输入工号/邮箱' : '请输入学号/邮箱'"
                size="large"
                class="login-input"
              />
            </a-form-item> -->
            <!--  密码 -->
            <!-- <a-form-item v-if="loginType === 1">
              <a-input-password
                v-model:value="registerForm.passWord"
                placeholder="请输入密码"
                prefix-icon="lock"
                size="large"
                class="login-input"
              />
            </a-form-item> -->
            <!-- 验证码注册 -->
            <!-- <a-form-item v-if="loginType === 2" :wrapper-col="{ span: 28 }">
              <div style="display: flex; align-items: center; justify-content: space-between">
                <a-input
                  v-model:value="registerForm.code"
                  placeholder="请输入验证码"
                  class="login-input"
                  style="flex: 1; margin-right: 10px"
                />
                <a-button
                  type="primary"
                  class="register-button"
                  style="width: auto; height: 40px"
                  @click="sendCode"
                >
                  {{ countdown > 0 ? `${countdown}秒` : '获取验证码' }}
                </a-button>
              </div>
            </a-form-item> -->

            <a-form-item>
              <a-button
                type="primary"
                class="register-button"
                block
                :loading="loading"
                html-type="submit"
              >
                {{ registerForm.isTeacher === 1 ? '教师注册' : '学生注册' }}
              </a-button>
            </a-form-item>

            <div class="login-link">
              已有账号？
              <router-link to="/user/login"> 立即登录 </router-link>
            </div>
          </a-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { Form, Input, Button, Checkbox, Link, Radio, Select } from 'ant-design-vue'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { registerUsingPost, getCollegeMajorUsingGet } from '../api/userController'
import { sendEmailUsingPost } from '../api/fasongyouxianggongnengApi'

//注册状态控制
const loading = ref(false)
const countdown = ref(0)

// 学院选项数组
const collegeOptions = ref<{ label: string; value: string }[]>([])

// 注册表单
const registerForm = reactive({
  name: '',
  number: '',
  college: '',
  email: '',
  password: '',
  code: '',
  isTeacher: 0,
})

// 获取学院数据
const fetchCollegeData = async () => {
  try {
    const res = await getCollegeMajorUsingGet({})
    if (res.data?.code === 200 && res.data.data) {
      // 获取前四个学院数据
      const topFourColleges = res.data.data.slice(0, 4)
      // 格式化选项数据
      collegeOptions.value = topFourColleges.map((item: any) => ({
        label: item.collegeName || item.name || item.value,
        value: item.id || item.code || item.value,
      }))
    }
  } catch (error) {
    console.error('获取学院数据失败:', error)
    message.error('获取学院数据失败')
  }
}

// 组件挂载时获取学院数据
onMounted(() => {
  fetchCollegeData()
})

const router = useRouter()

// 发送验证码通用方法
const sendVerificationCode = async (email) => {
  if (!email) {
    message.error('请输入邮箱')
    return
  }

  try {
    loading.value = true
    // 传递包含email属性的对象，而不是直接传递字符串
    const res = await sendEmailUsingPost({ email })
    //邮箱正则表达式校验
    const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/
    if (!emailRegex.test(registerForm.email)) {
      message.error('请输入正确的邮箱地址')
      return
    }
    if (res.data?.code === 200) {
      message.success('验证码已发送，请查收')
      // 设置倒计时
      countdown.value = 60
      const intervalId = setInterval(() => {
        if (countdown.value > 0) {
          countdown.value--
        } else {
          clearInterval(intervalId)
        }
      }, 1000)
    } else {
      message.error(res.data?.msg || '验证码发送失败，请稍后重试')
    }
  } catch (error) {
    console.error('验证码发送失败:', error)
    message.error('验证码发送失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 发送验证码
const sendCode = () => {
  // 检查是否输入了内容
  if (!registerForm.email) {
    message.error('请输入邮箱')
    return
  }

  // 检查输入是否为邮箱格式
  if (registerForm.email.includes('@')) {
    // 调用已有的发送验证码通用方法
    sendVerificationCode(registerForm.email)
  } else {
    message.error('请输入正确的邮箱地址')
  }
}

// 注册处理函数
const handleRegister = async (values) => {
  try {
    loading.value = true

    const res = await registerUsingPost({
      name: values.name,
      number: values.number,
      email: values.email,
      password: values.password,
      code: values.code,
      isTeacher: registerForm.isTeacher,
    })

    if (res.data?.code === 200 && res.data?.data) {
      message.success('注册成功!请登录')
      // 只有在注册成功时才跳转到登录页面
      router.push('/user/login')
    } else {
      // 确保显示后端返回的错误信息
      message.error(res.data?.msg || '注册失败，请稍后重试')
      // 注册失败时不跳转页面，让用户可以看到错误信息并修正
    }
  } catch (error) {
    console.error('注册失败:', error)
    message.error('注册失败，请稍后重试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
#userRegisterPage {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.register-container {
  display: flex;
  width: 100%;
  height: 100%;
}

/* 左侧品牌区域样式 */
.brand-section {
  width: 60%;
  background: linear-gradient(135deg, #5eead4, #2dd4bf);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.brand-section::before,
.brand-section::after {
  content: '';
  position: absolute;
  border-radius: 50%;
  opacity: 0.2;
}

.brand-section::before {
  width: 400px;
  height: 400px;
  background: rgba(255, 255, 255, 0.1);
  top: -100px;
  right: -100px;
}

.brand-section::after {
  width: 300px;
  height: 300px;
  background: rgba(255, 255, 255, 0.1);
  bottom: -50px;
  left: -50px;
}

.brand-content {
  text-align: center;
  color: white;
  z-index: 1;
}

.brand-name {
  font-size: 48px;
  font-weight: bold;
  margin-bottom: 8px;
  color: #06b6d4;
}

.brand-subtitle {
  font-size: 20px;
  margin-bottom: 30px;
  opacity: 0.9;
}

.brand-dots {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 30px;
}

.dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background-color: white;
  opacity: 0.7;
}

.brand-slogan {
  font-size: 18px;
  margin-bottom: 8px;
  opacity: 0.9;
}

.brand-mission {
  font-size: 16px;
  opacity: 0.8;
}

/* 右侧注册表单区域样式 */
.register-form-section {
  width: 40%;
  background-color: #f8fafc;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form-container {
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  padding: 40px;
  width: 100%;
  height: 650px;
  max-width: 400px;
}

.form-title {
  text-align: center;
  margin-bottom: 8px;
  font-size: 24px;
}

.title-primary {
  color: #06b6d4;
  font-weight: bold;
}

.title-secondary {
  color: #64748b;
  font-weight: normal;
  font-size: 16px;
  margin-left: 8px;
}

.form-subtitle {
  text-align: center;
  color: #334155;
  margin-bottom: 30px;
  font-size: 18px;
}

.login-form {
  width: 100%;
}

.login-input {
  width: 100%;
  height: 40px;
  border-radius: 6px;
}

.login-type-switch {
  display: flex;
  justify-content: center;
  margin-bottom: 16px;
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.login-type-switch-link {
  color: #1677ff; /* 蓝色文字 */
  cursor: pointer;
  transition: all 0.3s ease;
}

.login-type-switch-link:hover {
  color: #4096ff; /* 悬浮时更深的蓝色 */
  text-decoration: underline;
}

.register-button {
  width: 100%;
  height: 40px;
  font-size: 16px;
  background: linear-gradient(135deg, #4bd4bf, #2dd4bf);
  border: none;
  border-radius: 6px;
}

.register-button:hover {
  opacity: 0.9;
}

.login-link {
  text-align: center;
  margin-top: 20px;
  color: #64748b;
  font-size: 14px;
}

/* 添加一些装饰性的小点 */
.brand-section {
  position: relative;
}

.brand-section .decorative-dot {
  position: absolute;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: white;
  opacity: 0.4;
}

.brand-section .decorative-dot:nth-child(1) {
  top: 10%;
  left: 20%;
}

.brand-section .decorative-dot:nth-child(2) {
  top: 15%;
  left: 70%;
}

.brand-section .decorative-dot:nth-child(3) {
  top: 30%;
  left: 30%;
}

.brand-section .decorative-dot:nth-child(4) {
  top: 50%;
  left: 10%;
}

.brand-section .decorative-dot:nth-child(5) {
  top: 70%;
  left: 80%;
}

.brand-section .decorative-dot:nth-child(6) {
  top: 85%;
  left: 40%;
}

.brand-section .decorative-dot:nth-child(7) {
  top: 60%;
  left: 60%;
}
</style>
